home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ für Kids
/
C++ for kids.iso
/
SETUP
/
US
/
CBUILDER
/
DATA.Z
/
VECTOR.H
< prev
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
33KB
|
1,091 lines
#ifndef __STD_VECTOR__
#define __STD_VECTOR__
/* $Revision: 8.1 $ */
/***************************************************************************
*
* vector - declarations for the Standard Library vector class
*
* $Id: vector,v 1.41 1995/09/14 23:53:38 lijewski Exp $
*
***************************************************************************
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
***************************************************************************
*
* (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
* ALL RIGHTS RESERVED
*
* The software and information contained herein are proprietary to, and
* comprise valuable trade secrets of, Rogue Wave Software, Inc., which
* intends to preserve as trade secrets such software and information.
* This software is furnished pursuant to a written license agreement and
* may be used, copied, transmitted, and stored only in accordance with
* the terms of such license and with the inclusion of the above copyright
* notice. This software and information or any other copies thereof may
* not be provided or otherwise made available to any other person.
*
* Notwithstanding any other lease or license that may pertain to, or
* accompany the delivery of, this computer software and information, the
* rights of the Government regarding its use, reproduction and disclosure
* are as set forth in Section 52.227-19 of the FARS Computer
* Software-Restricted Rights clause.
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7013.
* Contractor/Manufacturer is Rogue Wave Software, Inc.,
* P.O. Box 2328, Corvallis, Oregon 97339.
*
* This computer software and information is distributed with "restricted
* rights." Use, duplication or disclosure is subject to restrictions as
* set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
* Computer Software-Restricted Rights (April 1985)." If the Clause at
* 18-52.227-74 "Rights in Data General" is specified in the contract,
* then the "Alternate III" clause applies.
*
**************************************************************************/
#include <stdcomp.h>
#include <function>
#include <iterator>
#include <algorith>
#ifndef Allocator
#define Allocator allocator
#include <memory>
#endif
#ifndef vector
#define vector vector
#endif
#ifndef RWSTD_NO_NAMESPACE
namespace std {
#endif
template <class T>
class vector
{
public:
//
// Types.
//
typedef Allocator<T> vector_allocator;
typedef typename vector_allocator::reference reference;
typedef typename vector_allocator::const_reference const_reference;
typedef typename vector_allocator::pointer iterator;
typedef typename vector_allocator::const_pointer const_iterator;
typedef typename vector_allocator::size_type size_type;
typedef typename vector_allocator::difference_type difference_type;
typedef T value_type;
typedef reverse_iterator<const_iterator, value_type,
const_reference, difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type,
reference, difference_type>
reverse_iterator;
protected:
Allocator<T> the_allocator;
iterator start;
iterator finish;
iterator end_of_storage;
void insert_aux (iterator position, const T& x);
public:
//
// construct/copy/destroy
//
vector () : start(0), finish(0), end_of_storage(0) {}
//
// Build a vector of size n with each element set to default for type T.
// This requires that T have a default constructor.
//
explicit vector (size_type n)
{
start = the_allocator.allocate(n);
T value;
uninitialized_fill_n(start, n, value);
finish = start + n;
end_of_storage = finish;
}
//
// Build a vector of size n with each element set to copy of value.
//
explicit vector (size_type n, const T& value)
{
start = the_allocator.allocate(n);
uninitialized_fill_n(start, n, value);
finish = start + n;
end_of_storage = finish;
}
vector (const vector<T>& x)
{
start = the_allocator.allocate(x.end() - x.begin());
finish = uninitialized_copy(x.begin(), x.end(), start);
end_of_storage = finish;
}
#ifndef RWSTD_NO_MEMBER_TEMPLATES
template<class InputIterator>
vector (InputIterator first, InputIterator last)
#else
vector (const_iterator first, const_iterator last)
#endif
{
size_type n;
__initialize(n, size_type(0));
distance(first, last, n);
start = the_allocator.allocate(n);
finish = uninitialized_copy(first, last, start);
end_of_storage = finish;
}
~vector ()
{
destroy(start, finish); the_allocator.deallocate(start);
}
vector<T>& operator= (const vector<T>& x);
#ifndef RWSTD_NO_MEMBER_TEMPLATES
template<class InputIterator>
void assign (InputIterator first, InputIterator last)
#else
void assign (const_iterator first, const_iterator last)
#endif
{
erase(begin(), end()); insert(begin(), first, last);
}
//
// Assign n copies of default value of type T to vector.
// This requires that T have a default constructor.
//
#ifndef RWSTD_NO_MEMBER_TEMPLATES
template<class Size, class T>
void assign (Size n)
#else
void assign (size_type n)
#endif
{
erase(begin(), end()); insert(begin(), n, T());
}
//
// Assign n copies of t to this vector.
//
#ifndef RWSTD_NO_MEMBER_TEMPLATES
template<class Size, class T>
void assign (Size n, const T& t)
#else
void assign (size_type n, const T& t)
#endif
{
erase(begin(), end()); insert(begin(), n, t);
}
//
// Iterators.
//
iterator begin () { return start; }
const_iterator begin () const { return start; }
iterator end () { return finish; }
const_iterator end () const { return finish; }
reverse_iterator rbegin ()
{
reverse_iterator tmp(end()); return tmp;
}
const_reverse_iterator rbegin () const
{
const_reverse_iterator tmp(end()); return tmp;
}
reverse_iterator rend ()
{
reverse_iterator tmp(begin()); return tmp;
}
const_reverse_iterator rend () const
{
const_reverse_iterator tmp(begin()); return tmp;
}
//
// Capacity.
//
size_type size () const { return size_type(end() - begin()); }
size_type max_size () const { return the_allocator.max_size(); }
void resize (size_type new_size);
void resize (size_type new_size, T value);
size_type capacity () const { return size_type(end_of_storage - begin()); }
bool empty () const { return begin() == end(); }
void reserve (size_type n)
{
if (capacity() < n)
{
iterator tmp = the_allocator.allocate(n);
uninitialized_copy(begin(), end(), tmp);
destroy(start, finish);
the_allocator.deallocate(start);